﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "ConfigDataModule.h"
#include <iostream>

#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
#include "IOSUtils.h"
#endif

static ConfigDataModule* configDataModule = NULL;


ConfigDataModule::ConfigDataModule(void)
{
}


ConfigDataModule::~ConfigDataModule(void)
{
}

ConfigDataModule* ConfigDataModule::getInstance(){
	if (configDataModule == NULL){
		configDataModule = new ConfigDataModule();
	}

	return configDataModule;
}

void ConfigDataModule::init(){
	//CCLog("加载配置数据");

	std::string sourceData = CCFileUtils::sharedFileUtils()->getFileString("data/source_zh_CN.json");

	if (!sourceData.empty() && sourceData != ""){
		CSJson::Reader read;
		CSJson::Value root;
		bool succ = read.parse(sourceData, root);

		if (succ)
		{
			initSource(root);
		}
		else{
			CCLog("source配置数据读取失败 \n");
		}
	}

	std::string configData = CCFileUtils::sharedFileUtils()->getFileString("data/config_zh_CN.json");

	if (!configData.empty() && configData != ""){
		CSJson::Reader read;
		CSJson::Value root;
		bool succ = read.parse(configData, root);

		if (succ)
		{
			initCityData(root);
			initSalaryRangeData(root);
			initSharePlatformData(root);
			initGoodsGrabSite(root);
			initGDTopicIcons(root);
			initGoodsTypes(root);
			initImageFilters(root);
			initImageStickers(root);
		}
		else{
			CCLog("config配置数据读取失败 \n");
		}
	}
}

void ConfigDataModule::initSource(CSJson::Value root){
	//if (IS_DEBUG) CCLog("加载语音文字配置数据");

	if (!root["sourceList"].empty()){
		const CSJson::Value &datalist = root["sourceList"];

		for (int i = 0; i < datalist.size(); i++) {
			source[datalist[i]["key"].asString()] = datalist[i]["title"].asString();
		}
	}
    
    sourceConfig = root["sourceObject"];

}

void ConfigDataModule::initCityData(CSJson::Value root){
	//if (IS_DEBUG) CCLog("加载城市配置数据");
	
	if (!root["cityList"].empty()){
		const CSJson::Value &datalist = root["cityList"];

		for (int i = 0; i < datalist.size(); i++) {
			GDCity city = parseGDCity(datalist[i]);
					
			if (!city.isNotNull || city.code.empty() || city.code == "" || city.code == "0"){
				continue;
			}

			if (city.hasParentCode()){ // 如果是二级城市
				vector<GDCity> subCityVector = subCitys[city.parentCode];
				subCityVector.push_back(city);
				subCitys[city.parentCode] = subCityVector;
			}
			else{ // 是一级城市
				topCitys.push_back(city);
			}
		}
	}
}

void ConfigDataModule::initSalaryRangeData(CSJson::Value root){
	//if (IS_DEBUG) CCLog("加载薪水范围配置数据");

	if (!root["salaryRangeList"].empty()){
		const CSJson::Value &datalist = root["salaryRangeList"];

		for (int i = 0; i < datalist.size(); i++) {
			GDSalaryRange salaryRange = parseGDSalaryRange(datalist[i]);

			if (!salaryRange.isNotNull){
				continue;
			}

			salaryRanges.push_back(salaryRange);
		}
	}
}

void ConfigDataModule::initSharePlatformData(CSJson::Value root){
	//if (IS_DEBUG) CCLog("加载分享平台配置数据");

	if (!root["sharePlatformList"].empty()){
		const CSJson::Value &datalist = root["sharePlatformList"];

		for (int i = 0; i < datalist.size(); i++) {
			GDSharePlatform salaryRange = parseGDSharePlatform(datalist[i]);

			if (!salaryRange.isNotNull){
				continue;
			}
            
            #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
            if (!IOSUtils::isWXInstalled()) {
                if (salaryRange.id == CCString::createWithFormat("%d",cn::sharesdk::C2DXPlatTypeWeixiSession)->getCString() ||
                    salaryRange.id == CCString::createWithFormat("%d",cn::sharesdk::C2DXPlatTypeWeixiTimeline)->getCString()) {
                    continue;
                }
            }
            if (!IOSUtils::isQQInstalled()) {
                if (salaryRange.id == CCString::createWithFormat("%d",cn::sharesdk::C2DXPlatTypeQZone)->getCString()||
                    salaryRange.id ==CCString::createWithFormat("%d",cn::sharesdk::C2DXPlatTypeQQ)->getCString()) {
                    continue;
                }
            }
            #endif

			sharePlatforms.push_back(salaryRange);
		}
	}
}

void ConfigDataModule::initGoodsGrabSite(CSJson::Value root){
	//if (IS_DEBUG) CCLog("加载商品分享网站配置数据");

	if (!root["goodsGrabSiteList"].empty()){
		const CSJson::Value &datalist = root["goodsGrabSiteList"];

		for (int i = 0; i < datalist.size(); i++) {
			GDGoodsGrabSite goodsGrabSite = parseGDGoodsGrabSite(datalist[i]);

			if (!goodsGrabSite.isNotNull){
				continue;
			}

			goodsGrabSites.push_back(goodsGrabSite);
		}
	}
}

void ConfigDataModule::initGDTopicIcons(CSJson::Value root){
	//if (IS_DEBUG) CCLog("加载专题图标配置数据");
	if (!root["topicIcons"].empty()){
		const CSJson::Value &datalist = root["topicIcons"];

		for (int i = 0; i < datalist.size(); i++) {
			GDTopicIcon item = parseGDTopicIcon(datalist[i]);

			if (!item.isNotNull){
				continue;
			}

			topicIcons[item.id] = item;
		}
	}
}

void ConfigDataModule::initGoodsTypes(CSJson::Value root){
	//if (IS_DEBUG) CCLog("加载货币单位配置数据");
	if (!root["goodsType"].empty()){
		const CSJson::Value &datalist = root["goodsType"];

		for (int i = 0; i < datalist.size(); i++) {
			GdBean item = parseGdBean(datalist[i]);

			if (!item.isNotNull){
				continue;
			}

			goodsTypes.push_back(item);
		}
	}
}

void ConfigDataModule::initImageFilters(CSJson::Value root)
{
	//if (IS_DEBUG) CCLog("加载滤镜配置数据");
	if (!root["imageFilters"].empty()){
		const CSJson::Value &datalist = root["imageFilters"];

		for (int i = 0; i < datalist.size(); i++) {
			GDImageFilter item = parseGDImageFilter(datalist[i]);

			if (!item.isNotNull){
				continue;
			}

			imageFilters.push_back(item);
		}
	}
}

void ConfigDataModule::initImageStickers(CSJson::Value root)
{
	if (!root["imageStickers"].empty()){
		const CSJson::Value &datalist = root["imageStickers"];

		for (int i = 0; i < datalist.size(); i++) {
			GdBean item = parseGdBean(datalist[i],"id","strId","name","value","ext");

			if (!item.isNotNull){
				continue;
			}

			imageStickers.push_back(item);
		}
	}
}

vector<GDCity> ConfigDataModule::getSubCity(std::string code){
	vector<GDCity> subCitysVect;
	if (code.empty() || code == ""){
		return subCitysVect;
	}

	subCitysVect = subCitys[code];

	return subCitysVect;
}

std::string ConfigDataModule::getSource(const std::string key, const std::string& defaultValue){
	std::string values = source[key];
	if (values.empty()){
		values = defaultValue;
	}
	return values;
}

CSJson::Value ConfigDataModule::getObjectSource(){
    return sourceConfig;
}